package com.russell.pms.webapp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.russell.pms.model.Property;
import com.russell.pms.service.PropertyService;
import com.russell.pms.webapp.bean.PropertyList;
import com.russell.pms.webapp.bean.StatusMessage;

@Controller
public class PropertyController extends BaseController {

	@Autowired
	private PropertyService propertyService;

	@RequestMapping(method = RequestMethod.GET, value = "/properties/{id}", headers = "Accept=application/xml, application/json")
	@ResponseBody
	public 	ResponseEntity<Property> getProperty(@PathVariable int id) {
		Property property = propertyService.getProperty(id);
		return new ResponseEntity<Property>(property, HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/properties", headers = "Accept=application/xml, application/json")
	@ResponseBody
	public 	ResponseEntity<PropertyList> getPropertyList() {
		List<Property> properties = propertyService.getProperties();
		PropertyList list = new PropertyList(properties);
		return new ResponseEntity<PropertyList>(list, HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.DELETE, value = "/properties/{id}", headers = "Accept=application/xml, application/json")
	@ResponseBody
	public 	ResponseEntity<StatusMessage> removeProperty(@PathVariable int id) throws Exception {
		propertyService.removeProperty(id);
		StatusMessage sm = new StatusMessage();
		sm.setStatus(HttpStatus.OK.value());
		sm.setMessage("Successful delete " + id);
		return new ResponseEntity<StatusMessage>(sm, HttpStatus.OK);

	}

	@RequestMapping(method = RequestMethod.POST, value = "/properties", headers = "Accept=application/xml, application/json")
	@ResponseBody
	public ResponseEntity<StatusMessage> saveProperty(@RequestBody Property property) {
		propertyService.saveProperty(property);

		StatusMessage sm = new StatusMessage();
		sm.setStatus(HttpStatus.CREATED.value());
		sm.setMessage("Accepted " + property.getId());
		return new ResponseEntity<StatusMessage>(sm, HttpStatus.CREATED);
	}

	@RequestMapping(method = RequestMethod.PUT, value = "/properties", headers = "Accept=application/xml, application/json")
	@ResponseBody
	public ResponseEntity<StatusMessage> updateProperty(@RequestBody Property property) {
		propertyService.updateProperty(property);
		StatusMessage sm = new StatusMessage();
		sm.setStatus(HttpStatus.OK.value());
		sm.setMessage("Updated " + property.getId());
		return new ResponseEntity<StatusMessage>(sm, HttpStatus.OK);
	}
	
	public void setPropertyService(PropertyService propertyService) {
		this.propertyService = propertyService;
	}

}
